package com.atguigu.chapter05.Transform;

import com.atguigu.chapter05.WaterSensor;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/3/3 15:29
 */
public class Flink03_Flatmap {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // TODO Transform  -  Flatmap ： 一进多出、零出，  同样有富函数版本
        DataStreamSource<List<Integer>> inputDS = env.fromElements(
                Arrays.asList(1, 2, 3),
                Arrays.asList(4, 5, 6),
                Arrays.asList(7, 8, 9)

        );

        inputDS
                .flatMap(new FlatMapFunction<List<Integer>, String>() {
                    @Override
                    public void flatMap(List<Integer> value, Collector<String> out) throws Exception {
                        for (Integer element : value) {
//                            out.collect(element.toString());

                            // 通过 采集器 实现类似 过滤的功能
                            if (element > 5){
                                out.collect(element.toString());
                            }
                        }
                    }
                })
                .print();

        env.execute();
    }
}
